<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pre_Controller
{
	public function __get($var)	{	return get_instance()->$var; }
	//public function __set($key, $val)	{	return get_instance()->$key = $val; }
	
	public function load()
	{
		$this->load->database();
		$this->configuration();
		$this->user();
		$this->languages();
		$this->modules();
		
	}
	
	private function modules()
	{
		$this->load->library('radicms_modules', '', 'modules');
	}
	

	
	private function configuration()
	{
		//$this->load->model('config_model');
		$config = $this->db->get('config')->result_array();
		if(!empty($config))
			foreach($config as $item)
				$this->config->set_item($item['name'], $item['value']);
				
		// Ion auth config hack and load into main stuff
		$this->load->config('ion_auth', TRUE);
		$this->config->set_item('site_title', $this->config->item('site_name'), 'ion_auth');
		$db_items = array( 'admin_email', 'default_group', 'admin_group', 'email_activation');
		foreach($db_items as $key)
			$this->config->set_item($key, $this->config->item($key), 'ion_auth');
		
	}
	
	
	private function user()
	{
		if($this->ion_auth->logged_in())
		{
			$user = $this->ion_auth->user()->row_array();
			$group = $this->ion_auth->get_users_groups($user['id'])->row_array();
			$group = $this->ion_auth->group($group['id'])->row_array();
			foreach($group as $name => $value)
			{
				if(strstr($name, 'cms_') !== FALSE)
				{
					$this->config->set_item($name, $value);		
				}
			}
			$this->config->set_item('user', $user);
			$this->config->set_item('group', $group);
			
		}
	}
	
	
	
	private function languages()
	{         
	    
		$this->load->helper('language');
		$this->load->helper('cookie');
		
		// Get (or set) the language		
		$lang = get_cookie('lang');
		if(!$lang || !in_array($lang, $this->config->item('cms_supported_languages')))
		{	
			$lang = $this->config->item('cms_default_language');
			$cookie = array(
					   'name'   => 'lang',
					   'value'  => $lang,
					   'expire' => '8650000'
				   );
	
			set_cookie($cookie); 
		}		
		$this->lang->load('main', $lang);
		$this->config->set_item('language', $lang);
	}
	
	
	
}
?>